package com.kede.app.service.impl;

import com.kede.app.domain.SuppliesStore;
import com.kede.utils.ValidationUtil;
import com.kede.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.kede.app.repository.SuppliesStoreRepository;
import com.kede.app.service.SuppliesStoreService;
import com.kede.app.service.dto.SuppliesStoreDto;
import com.kede.app.service.dto.SuppliesStoreQueryCriteria;
import com.kede.app.service.mapstruct.SuppliesStoreMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import com.kede.utils.PageUtil;
import com.kede.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @description 服务实现
* @author Pr.li
* @date 2023-06-06
**/
@Service
@RequiredArgsConstructor
public class SuppliesStoreServiceImpl implements SuppliesStoreService {

    private final SuppliesStoreRepository suppliesStoreRepository;
    private final SuppliesStoreMapper suppliesStoreMapper;

    @Override
    public Map<String,Object> queryAll(SuppliesStoreQueryCriteria criteria, Pageable pageable){
        Page<SuppliesStore> page = suppliesStoreRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(suppliesStoreMapper::toDto));
    }

    @Override
    public List<SuppliesStoreDto> queryAll(SuppliesStoreQueryCriteria criteria){
        return suppliesStoreMapper.toDto(suppliesStoreRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public SuppliesStoreDto findById(Integer id) {
        SuppliesStore suppliesStore = suppliesStoreRepository.findById(id).orElseGet(SuppliesStore::new);
        ValidationUtil.isNull(suppliesStore.getId(),"SuppliesStore","id",id);
        return suppliesStoreMapper.toDto(suppliesStore);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SuppliesStoreDto create(SuppliesStore resources) {
        return suppliesStoreMapper.toDto(suppliesStoreRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SuppliesStore resources) {
        SuppliesStore suppliesStore = suppliesStoreRepository.findById(resources.getId()).orElseGet(SuppliesStore::new);
        ValidationUtil.isNull( suppliesStore.getId(),"SuppliesStore","id",resources.getId());
        suppliesStore.copy(resources);
        suppliesStoreRepository.save(suppliesStore);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            suppliesStoreRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<SuppliesStoreDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (SuppliesStoreDto suppliesStore : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("门店id", suppliesStore.getStoreId());
            map.put("初始给到门店的数量", suppliesStore.getInitNum());
            map.put("物资id", suppliesStore.getSupplieId());
            map.put("物资名称 做冗余处理", suppliesStore.getSupplieName());
            map.put("当前剩余的数量", suppliesStore.getCurrNum());
            map.put("经度", suppliesStore.getLng());
            map.put("维度", suppliesStore.getLat());
            map.put(" createBy",  suppliesStore.getCreateBy());
            map.put(" updateBy",  suppliesStore.getUpdateBy());
            map.put(" createTime",  suppliesStore.getCreateTime());
            map.put(" updateTime",  suppliesStore.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}